package com.xingda.packing.minitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.common.R;
import com.xingda.packing.minitor.dto.WheelInfoDto;
import com.xingda.packing.minitor.entity.PkCacheLocation;
import com.xingda.packing.minitor.entity.PkDownWheel;
import com.xingda.packing.minitor.entity.PkTaskInfo;
import com.xingda.packing.minitor.entity.PkWheel;
import com.xingda.packing.minitor.service.IPkCacheLocationService;
import com.xingda.packing.minitor.service.IPkDownWheelService;
import com.xingda.packing.minitor.service.IPkTaskInfoService;
import com.xingda.packing.minitor.service.IPkWheelService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/wheel")
public class PkWheelSearchController {
    @Resource
    private IPkDownWheelService iPkDownWheelService;
    @Resource
    private IPkWheelService iPkWheelService;
    @Resource
    private IPkCacheLocationService iPkCacheLocationService;

    @Resource
    private IPkTaskInfoService iPkTaskInfoService;

    @GetMapping("/search")
    public R search(String code) {
        if (Objects.isNull(code) || "".equals(code.trim())) {
            return R.error("无条码参数-code");
        }
        LambdaQueryWrapper<PkDownWheel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PkDownWheel::getBarCode, code);
        PkDownWheel pkDownWheel = iPkDownWheelService.getOne(lambdaQueryWrapper);
        if (Objects.nonNull(pkDownWheel)) {
            return R.ok("工字轮{" + code + "}已经卸轮正在链板线");
        }
        LambdaQueryWrapper<PkWheel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PkWheel::getBarCode, code);
        queryWrapper.orderByDesc(PkWheel::getId);
        PkWheel pkWheel = iPkWheelService.list(queryWrapper).get(0);
        if (Objects.isNull(pkWheel)) {
            return R.error("工字轮 {" + code + "}信息不存在");
        } else {
            LambdaQueryWrapper<PkCacheLocation> query = new LambdaQueryWrapper<>();
            query.eq(PkCacheLocation::getWheelId, pkWheel.getId());
            PkCacheLocation pkCacheLocation = iPkCacheLocationService.getOne(query);
            if (Objects.nonNull(pkCacheLocation)) {
                if (pkCacheLocation.getArea() == 2) {
                    return R.ok("工字轮 {" + code + "}在右侧缓存区" + pkCacheLocation.getLocationNo());
                } else {
                    return R.ok("工字轮 {" + code + "}在左侧缓存区" + pkCacheLocation.getLocationNo());
                }
            } else {
                LambdaQueryWrapper<PkTaskInfo> pkTaskInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
                pkTaskInfoLambdaQueryWrapper.eq(PkTaskInfo::getWheelId, pkWheel.getId());
                PkTaskInfo pkTaskInfo = iPkTaskInfoService.getOne(pkTaskInfoLambdaQueryWrapper);
                if (Objects.isNull(pkTaskInfo)) {
                    return R.error("工字轮 {" + code + "}位置信息不存在");
                }
                WheelInfoDto dto = iPkTaskInfoService.getWheelLocation(code, pkWheel, pkTaskInfo);
                List<WheelInfoDto> dtoList = new ArrayList<>();
                dtoList.add(dto);
                return R.ok().put("data", dtoList);
            }
        }
    }
}
